package com.edu.shop.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.edu.shop.dao.UserDao;
import com.edu.shop.service.UserService;
import com.edu.shp.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public int register(User user) {
        // 使用MD5算法对密码进行加密
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        // 生成随机的盐值
        String salt = IdUtil.simpleUUID().substring(0,20);
        md5.setSalt(salt.getBytes());
        // 加盐后加密密码
        String md5Pass = md5.digestHex(user.getUpass());
        user.setUpass(md5Pass);
        // 设置用户信息中的盐值
        user.setSalt(salt);
        user.setCreateTime(new Date(System.currentTimeMillis()));
        // 保存用户信息到数据库
        return userDao.insert(user);
    }

    @Override
    public User findById(Integer id) {
        return userDao.selectById(id);
    }
}
